<template>
    <div class="container">
        <el-row>
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="标题：" prop="name" :rules="[{ required: true, message: '标题不能为空' }]">
                    <el-input v-model="form.name" @input="handleTitle" placeholder="请输入标题"></el-input>
                </el-form-item>
                <el-form-item label="收益率：" prop="revenueRate" :rules="[{ required: true, message: '收益率不能为空' }]">
                    <el-input style="width: 150px" v-model="form.revenueRate" placeholder="请输入收益率"></el-input>
                </el-form-item>
                <el-form-item label="服务期限：" prop="period" :rules="[{ required: true, message: '服务期限不能为空' }]">
                    <el-input style="width: 150px" v-model="form.period" placeholder="请输入服务期限"></el-input>
                </el-form-item>
                <el-form-item label="状态：" prop="status" :rules="[{ required: true, message: '标题不能为空' }]">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">在售项目</el-radio>
                        <el-radio :label="2">募集结束</el-radio>
                        <el-radio :label="3">月兑付表</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否滚动：" prop="isBanner" style="width: 300px; display: inline-block">
                    <el-switch v-model="form.isBanner" :active-value="1" :inactive-value="0"> </el-switch>
                </el-form-item>
                <el-form-item label="是否热销：" prop="isHot" style="width: 300px; display: inline-block">
                    <el-switch v-model="form.isHot" :active-value="1" :inactive-value="0"> </el-switch>
                </el-form-item>
                <el-form-item ref="img" label="滚动图：" prop="bannerUrl">
                    <Upload @fileListChange="bannerChange" ref="myUpload" :drag="true" :accept="imgType" fileType="image" />
                </el-form-item>
                <el-form-item ref="img" label="封面图：" prop="coverUrl">
                    <Upload @fileListChange="fileListChange" ref="myUpload" :drag="true" :accept="imgType" fileType="image" />
                </el-form-item>
                <!-- <el-form-item
                    class="radio-form"
                    label-width="100"
                    label="是否是banner："
                    prop="isBanner"
                    :rules="[{ required: true, message: '选项不能为空' }]"
                    >
                    <el-col :span="12">
                        <el-radio-group v-model="form.isBanner">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="0">否</el-radio>
                        </el-radio-group>
                    </el-col>
                </el-form-item> -->
                <el-form-item label="内容：" prop="content" :rules="[{ required: true, message: '内容不能为空' }]">
                    <tinymce v-model="form.content" :height="400" @input="handleContent" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit('form')" :loading="loading">保存</el-button>
                </el-form-item>
            </el-form>
        </el-row>
    </div>
</template>

<script>
    import normalFilter from "@/components/NormalFilter";
    import Upload from "@/components/UploadFile";
    import Tinymce from "@/components/Tinymce";
    import { Message } from "element-ui";
    import { add } from "@/api/product";
    import { imgTypeArr } from "@/utils/enumConfig";

    export default {
        name: "Lottery",
        components: {
            normalFilter,
            Tinymce,
            Upload,
        },
        data() {
            return {
                loading: false,
                imgType: imgTypeArr.join(","),
                form: {
                    name: "",
                    revenueRate: "",
                    period: 1,
                    status: 1,
                    content: "",
                    bannerUrl: "",
                    coverUrl: "",
                    isBanner: 0,
                    isHot: 0,
                },
            };
        },
        mounted() {
            //   tinymce.init({})
        },
        methods: {
            handleTitle(val) {
                this["form"]["title"] = val;
            },
            handleContent(val) {
                this["form"]["content"] = val;
            },
            onSubmit(formName) {
                const { validate } = this.$refs[formName];
                validate((valid) => {
                    if (valid) {
                        this._submitData(this.form);
                    }
                });
            },
            fileListChange(arr) {
                if (arr.length === 0) {
                    this["form"]["coverUrl"] = "";
                } else {
                    this["form"]["coverUrl"] = arr[0]["url"];
                    this.$refs.img.clearValidate();
                }
            },
            bannerChange(arr) {
                if (arr.length === 0) {
                    this["form"]["bannerUrl"] = "";
                } else {
                    this["form"]["bannerUrl"] = arr[0]["url"];
                    this.$refs.img.clearValidate();
                }
            },
            async _submitData(params) {
                this.loading = true;
                let res = await add(params);
                this.loading = false;
                if (res.code === 1) {
                    Message({
                        message: "保存成功！",
                        type: "success",
                        duration: 1 * 1000,
                    });
                    setTimeout(() => {
                        this.$router.go(-1);
                    }, 1000);
                }
            },
        },
    };
</script>

<style lang="scss" scoped>
    .container {
        width: 100%;
        height: 100%;
        padding: 20px;
        .radio-form {
            ::v-deep .el-form-item__error {
                left: 5%;
            }
        }
    }
</style>
